|
|
|
|
|
|
|
Solution 13
Registry Games, Part 3 |
|
|
|
|
|
|
|
|
You have a lot of new code and no suggestion as to where it is failing. In cases such as this, there is no substitution for stepping through the code carefully. And I stress the word ''carefully." That means not just looking at function results, but also searching for possible side effects. In this case, you will find a most perplexing situation on the following line: |
|
|
|
|
|
|
|
|
res = GetValueInfo(hKey, NumberOfSubValues, MaxSubValueLength) |
|
|
|
|
|
|
|
|
The function always returns a correct result (0), but never sets the NumberOfSubValues or MaxSubValueLength variables! We know from looking at the registry that many of the subkeys we are looking at do have values, so clearly there must be a problem. Let's take a closer look at the GetValueInfo function: |
|
|
|
|
|
|
|
|
' Get the number of values and maximum value length
Private Function GetValueInfo(ByVal hKey As Long, _
NumberOfValues, MaxValueNameLength) As Long
GetValueInfo = RegQueryInfoKeyV3(hKey, vbNullString, 0, 0, 0, 0, _
0, VarPtr(NumberOfValues), VarPtr(MaxValueNameLength), 0, 0, 0)
End Function |
|
|
|
|
|
|
|
|
How can the function consistently return a correct result, yet not work? The answer is, it can't. There must be a problem with this code. But what can the problem be? The function looks just like the GetKeyInfo3 function that we know works correctly. Let's take a second look at the GetKeyInfo3 function: |
|
|
|
|
|
|
|
|
Private Function GetKeyInfo3(ByVal hKey As Long, NumberOfKeys _
As Long, MaxKeyNameLength As Long) As Long
GetKeyInfo3 = RegQueryInfoKeyV3(hKey, vbNullString, 0, 0, _
VarPtr(NumberOfKeys), VarPtr(MaxKeyNameLength), 0, 0, 0, 0, 0, 0)
End Function |
|
|
|
|
|
|
|
|
Look closely now. Do you see it? |
|
|
|
|
|
|
|
|
The GetKeyInfo3 function declares the NumberOfKeys parameters as Long. We neglected to specify a parameter type in the GetValueInfo function. That means that the parameters are declared as variants. |
|
|
|
|
|